message(NOTICE "开始构建 ffmpeg 模块")
# 检查构建目标是否存在
if(TARGET ${BUILD_FFMPEG_TARGET})
    message(NOTICE "ffmpeg 模块已存在 ${BUILD_FFMPEG_TARGET}")
 return()
endif()

# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/ffmpeg/${FFMPEG_TAG}
    ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}
    ${BUILD_FFMPEG_TARGET})

# 构建配置文件
set(FFMPEG_CONFIGURE CACHE STRING "ffmpeg configure" FORCE)
# 配置 开启 ffmpeg 动态库静态库
if(BUILD_SHARED_LIBS)
    list(APPEND FFMPEG_CONFIGURE "--enable-shared")
else()
    list(APPEND FFMPEG_CONFIGURE "--disable-shared")
endif()
if(BUILD_STATIC_LIBS)
    list(APPEND FFMPEG_CONFIGURE "--enable-static")
else()
    list(APPEND FFMPEG_CONFIGURE "--disable-static")
endif()

# 配置 开启 ffmpeg 组件支持
if(WITH_ZLIB)
    list(APPEND FFMPEG_CONFIGURE "--enable-zlib")
endif()
if(WITH_OPENSSL)
    list(APPEND FFMPEG_CONFIGURE "--enable-openssl")
endif()
if(WITH_X264)
    list(APPEND FFMPEG_CONFIGURE "--enable-libx264")
endif()
if(WITH_X265)
    list(APPEND FFMPEG_CONFIGURE "--enable-libx265")
endif()
if(WITH_MP3LAME)
    list(APPEND FFMPEG_CONFIGURE "--enable-libmp3lame")
endif()
if(WITH_SPEEX)
    list(APPEND FFMPEG_CONFIGURE "--enable-libspeex")
endif()
if(WITH_FDK_AAC)
    list(APPEND FFMPEG_CONFIGURE "--enable-libfdk-aac")
endif()

# --- 调试专用配置 ---
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND FFMPEG_CONFIGURE "--enable-debug=3") # 开启调试信息
    list(APPEND FFMPEG_CONFIGURE "--disable-optimizations") # 禁用优化
    list(APPEND FFMPEG_CONFIGURE "--disable-small") # 禁用小优化
    list(APPEND FFMPEG_CONFIGURE "--disable-stripping") # 禁用 stripping
    list(APPEND FFMPEG_CONFIGURE "--disable-asm") # 禁用汇编
    list(APPEND FFMPEG_CONFIGURE "--disable-inline-asm") # 禁用内联汇编
    list(APPEND FFMPEG_CONFIGURE "--disable-neon")          # 避免汇编
    # --- 许可 ---
    list(APPEND FFMPEG_CONFIGURE "--enable-gpl")
    list(APPEND FFMPEG_CONFIGURE "--enable-nonfree")
    list(APPEND FFMPEG_CONFIGURE "--enable-version3")
# 配置 Release 发布版
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    # --- 性能优化 ---
    list(APPEND FFMPEG_CONFIGURE "--enable-gpl")
    list(APPEND FFMPEG_CONFIGURE "--enable-nonfree")
    list(APPEND FFMPEG_CONFIGURE "--enable-version3")
    list(APPEND FFMPEG_CONFIGURE "--enable-optimizations") # 启用优化
    list(APPEND FFMPEG_CONFIGURE "--enable-pthreads") # 启用线程
    list(APPEND FFMPEG_CONFIGURE "--enable-asm") # 启用汇编
    list(APPEND FFMPEG_CONFIGURE "--enable-inline-asm") # 启用内联汇编
    list(APPEND FFMPEG_CONFIGURE "--enable-neon") # 启用 NEON
    list(APPEND FFMPEG_CONFIGURE "--enable-hardcoded-tables") # 启用硬编码表
    list(APPEND FFMPEG_CONFIGURE "--enable-small") # 启用小优化
else()
  message(FATAL_ERROR "不支持的构建类型 ${CMAKE_BUILD_TYPE}")
endif()

# Android 配置 模块
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
    list(APPEND FFMPEG_CONFIGURE "--target-os=android")
    list(APPEND FFMPEG_CONFIGURE "--arch=${ARCH}")
    list(APPEND FFMPEG_CONFIGURE "--cpu=${CPU}")
    list(APPEND FFMPEG_CONFIGURE "--cc=${CMAKE_C_COMPILER}")
    list(APPEND FFMPEG_CONFIGURE "--cxx=${CMAKE_CXX_COMPILER}")
    list(APPEND FFMPEG_CONFIGURE "--sysroot=${CMAKE_SYSROOT}")
    list(APPEND FFMPEG_CONFIGURE "--enable-cross-compile")
    list(APPEND FFMPEG_CONFIGURE "--cross-prefix=${CROSS}")
# Linux 配置 模块
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    list(APPEND FFMPEG_CONFIGURE "--target-os=linux")
# Windows 配置 模块
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    list(APPEND FFMPEG_CONFIGURE "<SOURCE_DIR>/configure")
    list(APPEND FFMPEG_CONFIGURE "win64-x86_64")
else()
    message(FATAL_ERROR "不支持的系统类型 ${CMAKE_SYSTEM_NAME}")
endif()
# 创建
#add_custom_target
# 构建 speex 库
ExternalProject_Add(
    ${BUILD_FFMPEG_TARGET}
    # 目录配置
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                    # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/ffmpeg/${FFMPEG_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/ffmpeg/${FFMPEG_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/ffmpeg/${FFMPEG_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/ffmpeg/${FFMPEG_TAG}          # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${FFMPEG_REPO}
            -DGIT_TAG=${FFMPEG_TAG}
            -P ${__DIR}/utils/git.cmake

    CONFIGURE_COMMAND   # ✅ 正确的 CMake 配置命令
        env PKG_CONFIG_PATH=${SYSTEM_PREFIX}/lib/pkgconfig
       # env PKG_CONFIG_LIBDIR=${SYSTEM_PREFIX}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}
       # env PKG_CONFIG_SYSROOT_DIR=/
        <SOURCE_DIR>/configure            # 配置脚本
        --prefix=<INSTALL_DIR>            # 安装目录
        # --- 禁用无关程序 ---
        --disable-doc                      # 禁用文档生成
        --disable-programs                # 禁用程序生成
        --disable-ffmpeg                  # 禁用 ffmpeg 生成
        --disable-ffplay                  # 禁用 ffplay 生成
        --disable-ffprobe                  # 禁用 ffprobe 生成
        --disable-symver                  # 禁用符号版本
        # --- 核心功能 ---
        --disable-everything              # 禁用所有组件
        --enable-decoders                # 启用解码器
        --enable-encoders                # 启用编码器
        --enable-demuxer=h264,hevc,mpegts,mp4,aac,wav,pcm_s16le,rawvideo,mov,avi # 启用复用器
        --enable-muxer=h264,hevc,mpegts,mp4,aac,wav,pcm_s16le,rawvideo,mov,avi # 启用复用器
        --enable-parser=h264,h265,aac,mpeg4video,mpegvideo # 启用解析器
        --enable-protocol=file,http,https,rtmp,rtsp,rtp # 启用协议
        --enable-filter=scale,resample,crop,pad,format,aformat # 启用滤镜
        --pkg-config=pkg-config
        --pkg-config-flags=--static
       "--extra-cflags=-g3\ -O0\ -fno-omit-frame-pointer\ -fPIC\ -I${SYSTEM_PREFIX}/include" # 编译选项
       "--extra-ldflags=-s\ -Wl,--gc-sections\ -L${SYSTEM_PREFIX}/lib" # 链接选项
        ${FFMPEG_CONFIGURE}              # 配置参数
    # 构建命令
    BUILD_COMMAND make -j${NPROC}

    INSTALL_COMMAND # 安装命令
        COMMAND make install
            # 将安装文件路径保存到 文件去
            COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     ON                            # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   # 依赖目标 安装需要先安装 install-pkg-config
   DEPENDS install-pkg-config
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)
# 配置 依赖库
if(WITH_ZLIB)
    add_dependencies(${BUILD_FFMPEG_TARGET} zlib-${ZLIB_TAG})
endif()
if(WITH_OPENSSL)
    add_dependencies(${BUILD_FFMPEG_TARGET} openssl-${OPENSSL_TAG})
endif()
if(WITH_FDK_AAC)
    add_dependencies(${BUILD_FFMPEG_TARGET} fdk-aac-${FDK_AAC_TAG})
endif()
if(WITH_MP3LAME)
    add_dependencies(${BUILD_FFMPEG_TARGET} mp3lame-${MP3LAME_TAG})
endif()
if(WITH_SPEEX)
    add_dependencies(${BUILD_FFMPEG_TARGET} speex-${SPEEX_TAG})
endif()

if(WITH_X264)
    add_dependencies(${BUILD_FFMPEG_TARGET} x264-${X264_TAG})
endif()
if(WITH_X265)
    add_dependencies(${BUILD_FFMPEG_TARGET} x265-${X265_TAG})
endif()

# 1 导入组件 libavcodec.a
add_library(ffmpeg::avcodec STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::avcodec ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::avcodec PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libavcodec.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include
)

# 2 导入组件 libavdevice.a
add_library(ffmpeg::avdevice STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::avdevice ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::avdevice PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libavdevice.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)

# 3 导入组件 libavfilter.a
add_library(ffmpeg::avfilter STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::avfilter ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::avfilter PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libavfilter.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)

# 4 导入组件 libavformat.a
add_library(ffmpeg::avformat STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::avformat ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::avformat PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libavformat.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)

# 5 导入组件 libavutil.a
add_library(ffmpeg::avutil STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::avutil ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::avutil PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libavutil.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)

# 6 导入组件 libswresample.a
add_library(ffmpeg::swresample STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::swresample ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::swresample PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libswresample.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)

# 7 导入组件 libswscale.a
add_library(ffmpeg::swscale STATIC IMPORTED)
# 依赖目标
add_dependencies(ffmpeg::swscale ${BUILD_FFMPEG_TARGET})
# 属性设置
set_target_properties(ffmpeg::swscale PROPERTIES
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/lib/libswscale.a
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ffmpeg/${FFMPEG_TAG}/include)









